﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.IO;
using System.Text;
using System.Data;

namespace Booking_Hotel
{
    public class Code
    {
        public static string MaHoa(string key, string toEn)
        {
            byte[] keyArray;
            byte[] toEndcry = UTF8Encoding.UTF8.GetBytes(toEn);
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            keyArray = md5.ComputeHash(UTF32Encoding.UTF8.GetBytes(key));
            TripleDESCryptoServiceProvider trip = new TripleDESCryptoServiceProvider();
            trip.Key = keyArray;
            trip.Mode = CipherMode.ECB;
            trip.Padding = PaddingMode.PKCS7;
            ICryptoTransform tranform = trip.CreateEncryptor();
            byte[] resualArray = tranform.TransformFinalBlock(toEndcry, 0, toEndcry.Length);
            return Convert.ToBase64String(resualArray, 0, resualArray.Length);
        }
        //Phuong thuc giai ma bang MD5:
        public static string GiaiMa(string key, string toDe)
        {
            byte[] keyArray;
            byte[] toEndArray = Convert.FromBase64String(toDe);
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            keyArray = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
            TripleDESCryptoServiceProvider trip = new TripleDESCryptoServiceProvider();
            trip.Key = keyArray;
            trip.Mode = CipherMode.ECB;
            trip.Padding = PaddingMode.PKCS7;
            ICryptoTransform tranfrom = trip.CreateDecryptor();
            byte[] resualArray = tranfrom.TransformFinalBlock(toEndArray, 0, toEndArray.Length);
            return UTF32Encoding.UTF8.GetString(resualArray);

        }
        public Code()
        {
            //
            // TODO: Add constructor logic here
            //
        }
    }
}